/*
 * @Author: caijw 
 * @Date: 2018-11-25 13:53:48 
 * @Last Modified by: caijw
 * @Last Modified time: 2019-01-10 23:04:26
 * 
 * 获取用户信息 info
 */

import * as types from '../action-types';
import { getUserInfo, postSignInfo, postChangeName, postCard, 
  getCardList,getCollection, getView,getAbout,getMyMoney,bindAliCard,
  getMoney,getMessage,getOrder,postAddress,getAddress,deleteAddress,pathAddress,getAddressDetail } from '../../api/index';
//获取用户信息
export function fetchUser() {
  return function(dispatch) {
    //先获取数据显示加载内容
    dispatch({type: types.FETCH_USER});
    getUserInfo().then(payload=>{
      dispatch({type: types.FETCH_USER_FULFILLED, payload: payload});
    })
    
  }
}

//签到
export function postSign(){
  return function(dispatch) {
    //先获取数据显示加载内容
    dispatch({type: types.POST_SIGN});
    postSignInfo().then(payload=>{
      dispatch({type: types.POST_SIGN_FULFILLED, payload:payload});
    })
   
  }
}

//改变用户名
export function changeUserName(id,data){
  return function(dispatch) {
    //先获取数据显示加载内容
    dispatch({type: types.POST_CHANGE_NAME});
    postChangeName(id,data).then(payload=>{
      dispatch({type: types.POST_CHANGE_NAME_FULFILLED, payload: payload});
    })
   
  }
}


//充值卡
export function postCardNumber(data){
  return function(dispatch) {
    //先获取数据显示加载内容
    dispatch({type: types.POST_CARD});
    postCard(data).then(payload=>{
      dispatch({type: types.POST_CARD_FULFILLED, payload: payload});
    },err=>{
      dispatch({type: types.POST_CARD_FULFILLED, payload: payload});
    })
  }
}


//充值卡
export function getCardNumberList(data){
  return function(dispatch) {
    //先获取数据显示加载内容
    dispatch({type: types.GET_CARD_LIST});
    getCardList(data).then(payload=>{
      dispatch({type: types.GET_CARD_LIST_FULFILLED, payload: payload});
    })
   
  }
}

//收藏记录
export function getCollectionList(){
  return function(dispatch,getState) {
    let {page} = getState().my.collection;
    //先获取数据显示加载内容
    dispatch({type: types.GET_COLLECTION_LIST});
    getCollection(page).then(payload=>{
      dispatch({type: types.GET_COLLECTION_LIST_FULFILLED, payload:payload });
    }, err=>{
      dispatch({type: types.GET_COLLECTION_LIST_FULFILLED, payload:[] });
    })
  }
}

//浏览记录
export function getViewList(){
  return function(dispatch, getState) {
    let {page} = getState().my.view;
    //先获取数据显示加载内容
    dispatch({type: types.GET_VIEW_LIST});
    getView(page).then(payload=>{
      dispatch({type: types.GET_VIEW_LIST_FULFILLED, payload:payload });
    }, err=>{
      dispatch({type: types.GET_VIEW_LIST_FULFILLED, payload:[] });
    })
  }
}

//关于
export function fetchAbout(){
  return function(dispatch) {
    //先获取数据显示加载内容
    dispatch({type: types.GET_ABOUT});
    getAbout().then(payload=>{
      dispatch({type: types.GET_ABOUT_FULFILLED, payload:payload });
    }, err=>{
      dispatch({type: types.GET_ABOUT_FULFILLED, payload:{} });
    })
  }
}


//关于
export function fetchMyMoney(){
  return function(dispatch) {
    //先获取数据显示加载内容
    dispatch({type: types.GET_MY_MONEY});
    getMyMoney().then(payload=>{
      dispatch({type: types.GET_MY_MONEY_FULFILLED, payload:payload });
    }, err=>{
      dispatch({type: types.GET_MY_MONEY_FULFILLED, payload:{} });
    })
  }
}

//绑定支付宝
export function bindCard(data){
  return function(dispatch) {
    //先获取数据显示加载内容
    dispatch({type: types.BIND_CARD});
    bindAliCard(data).then(payload=>{
      if(payload.ret == 0){
        alert('绑定成功')
        getMyMoney().then(payload=>{
          dispatch({type: types.GET_MY_MONEY_FULFILLED, payload:payload });
        }, err=>{
          dispatch({type: types.GET_MY_MONEY_FULFILLED, payload:{} });
        })
      }
    }, err=>{
      if(err.response.data.ret == -1){
        alert(err.response.data.msg);
      }
      dispatch({type: types.BIND_CARD_FULFILLED, payload:{} });
    })
  }
}


//体现
export function fetchMoney(data){
  return function(dispatch) {
    //先获取数据显示加载内容
    dispatch({type: types.GET_MONEY});
    getMoney(data).then(payload=>{
      dispatch({type: types.GET_MONEY_FULFILLED, payload:payload });
    }, err=>{
      if(err.response.data.ret == -1){
        alert(err.response.data.msg);
      }
      dispatch({type: types.GET_MONEY_FULFILLED, payload:err.response.data });
    })
  }
}

//消息接口
export function fetchMessage(){
  return function(dispatch, getState) {
    let {page} = getState().my.message;
    //先获取数据显示加载内容
    dispatch({type: types.GET_MESSAGE});
    getMessage(page).then(payload=>{
      dispatch({type: types.GET_MESSAGE_FULFILLED, payload:payload });
    }, err=>{
      dispatch({type: types.GET_MESSAGE_FULFILLED, payload:{data:{push_message_list:[]}} });
    })
  }
}

//订单
export function fetchOrders(){
  return function(dispatch, getState) {
    let {page, type} = getState().my.order;
    //先获取数据显示加载内容
    dispatch({type: types.GET_ORDER});
    getOrder(page,type).then(payload=>{
      dispatch({type: types.GET_ORDER_FULFILLED, payload:payload });
    }, err=>{
      dispatch({type: types.GET_ORDER_FULFILLED, payload:{} });
    })
  }
}


//新增地址
export function AddAddress(data){
  return function(dispatch) {
    //先获取数据显示加载内容
    dispatch({type: types.POST_ADDRESS});
    postAddress(data).then(payload=>{
      alert('新增成功');
      dispatch({type: types.POST_ADDRESS_FULFILLED, payload: payload});
    },err=>{
      if(err.response.data.ret == -1){
        alert(err.response.data.msg);
      }
      dispatch({type: types.POST_ADDRESS_FULFILLED, payload: err.response.data});
    })
  }
}

//修改一个地址
export function pathAddressItem(id,data){
  return function(dispatch) {
    //先获取数据显示加载内容
    dispatch({type: types.PATH_ADDRESS});
    pathAddress(id,data).then(payload=>{
      alert('编辑成功');
      dispatch({type: types.PATH_ADDRESS_FULFILLED, payload: payload});

      dispatch({type: types.GET_ADDRESS});
      getAddress().then(payload=>{
        dispatch({type: types.GET_ADDRESS_FULFILLED, payload: payload});
      },err=>{
        if(err.response.data.ret == -1){
          alert(err.response.data.msg);
        }
        dispatch({type: types.GET_ADDRESS_FULFILLED, payload: err.response.data});
      })
    },err=>{
      dispatch({type: types.PATH_ADDRESS_FULFILLED, payload: err.response.data});
    })
  }
}

//获取地址列表
export function getAddressList(){
  return function(dispatch) {
    //先获取数据显示加载内容
    dispatch({type: types.GET_ADDRESS});
    getAddress().then(payload=>{
      dispatch({type: types.GET_ADDRESS_FULFILLED, payload: payload});
    },err=>{
      dispatch({type: types.GET_ADDRESS_FULFILLED, payload: err.response.data});
    })
  }
}
//删除地址
export function deleteAddressItem(id){
  return function(dispatch) {
    //先获取数据显示加载内容
    dispatch({type: types.DELETE_ADDRESS});
    deleteAddress(id).then(payload=>{
      dispatch({type: types.DELETE_ADDRESS_FULFILLED, payload: payload});

      dispatch({type: types.GET_ADDRESS});
      getAddress().then(payload=>{
        dispatch({type: types.GET_ADDRESS_FULFILLED, payload: payload});
      },err=>{
        dispatch({type: types.GET_ADDRESS_FULFILLED, payload: err.response.data});
      })
    },err=>{
      dispatch({type: types.DELETE_ADDRESS_FULFILLED, payload: err.response.data});
    })
  }
}

//获取地址明细
export function getAddressItem(id){
  return function async (dispatch) {
    //先获取数据显示加载内容
    dispatch({type: types.GET_ADDRESS_DETAIL});
    getAddressDetail(id).then(payload=>{
      dispatch({type: types.GET_ADDRESS_DETAIL_FULFILLED, payload: payload});
    },err=>{
      dispatch({type: types.GET_ADDRESS_DETAIL_FULFILLED, payload: err.response.data});
    })
  }
}
